package com.dd.shop4j.console.member.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dd.shop4j.common.utils.Page;
import com.dd.shop4j.console.member.controller.forms.MemberForm;
import com.dd.shop4j.member.mapper.MemberAddressMapper;
import com.dd.shop4j.member.mapper.MemberInfoMapper;
import com.dd.shop4j.member.model.MemberAddress;
import com.dd.shop4j.member.model.MemberInfo;

/**
 * 
 * 会员管理基础类 <br />
 * <ul style="list-style: decimal;">
 * <li>分页条件查询会员信息</li>
 * </ul>
 * 
 * @author Dan Hu
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MemberManagerService {
	@Resource private MemberInfoMapper memberInfoMapper;
	@Resource private MemberAddressMapper memberAddressMapper;
	
	
	/**
	 * 条件查询会员信息
	 * @param memberInfo
	 * @return
	 */
	public List<MemberInfo> queryMemberInfos(MemberInfo memberInfo) {
		return memberInfoMapper.queryBySelective(memberInfo);
	}

	/**
	 * 分页条件查询会员信息
	 * @param memberInfo	会员的查询条件
	 * @param curpage	当前页码
	 * @param pagesize	页大小
	 * @return
	 */
	public Page<MemberInfo> queryMemberInfoByPage(MemberInfo memberInfo,
			int curpage, int pagesize) {
		//分也条件查询出会员数据集
		List<MemberInfo> members = memberInfoMapper.queryMembersByPage(memberInfo, (curpage - 1) * pagesize, pagesize, "add_time", "desc");
		//查询出会员的总数量
		int count = memberInfoMapper.queryMembersCount(memberInfo);
		return new Page<MemberInfo>(members, count, curpage, pagesize);
	}
	
	/**
	 *
	 * 新增会员信息
	 *
	 * @param form
	 */
	public void insertMember(MemberForm form){
		MemberInfo memberInfo = form.getMemberInfo();
		memberInfo.setAddTime(new Date());
		memberInfo.setLevelId(1);
		memberInfo.setPassword("123456");
		memberInfoMapper.insertSelective(memberInfo);
	}
	
	/**
	 * 
	 * @param id
	 */
	public void deleteMember(Integer id){
		memberInfoMapper.deleteByPrimaryKey(id);
	}
	
	/**
	 * 根据商品id 加载会员信息
	 * @param memberId 会员id
	 * @return
	 */
	public MemberInfo loadMemberById(Integer memberId) {
		return memberInfoMapper.selectByPrimaryKey(memberId);
	}
	
	/**
	 * 更新会员基本信息
	 * 
	 * @param form
	 */
	public void updateMember(MemberForm form){
		MemberInfo memberInfo = form.getMemberInfo();
		//修改会员的基本信息
		memberInfoMapper.updateSelectiveByPrimaryKey(memberInfo);
		
	}
	
	/**
	 *
	 * 新增会员收货地址
	 *
	 * @param form
	 */
	public void insertAddress(MemberForm form){
		MemberAddress memberAddress = form.getMemberAddress();
		memberAddressMapper.insertSelective(memberAddress);
	}
	
	/**
	 * 删除会员的收货地址
	 * 
	 * @param id
	 */
	public void deleteMemberAddress(Integer id){
		memberAddressMapper.deleteByPrimaryKey(id);
	}
	
	/**
	 * 根据商品id 加载会员收货地址
	 * @param addressId 地址id
	 * @return
	 */
	public MemberAddress loadMemberAddressById(Integer addressId) {
		return memberAddressMapper.selectByPrimaryKey(addressId);
	}
	
	/**
	 * 更新会员收货地址
	 * 
	 * @param form
	 */
	public void updateMemberAddress(MemberForm form){
		MemberAddress memberAddress = form.getMemberAddress();
		//修改会员的收货地址
		memberAddressMapper.updateSelectiveByPrimaryKey(memberAddress);
	}
	
	/**
	 * 条件查询收货地址信息
	 * @param memberAddress
	 * @return
	 */
	public List<MemberAddress> queryMemberAddress(MemberAddress memberAddress) {
		return memberAddressMapper.queryBySelective(memberAddress);
	}
}
